//
//  TotalCityViewController.m
//  PTravel
//
//  Created by dllo on 15/11/19.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "TotalCityViewController.h"

@interface TotalCityViewController ()<UICollectionViewDataSource, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (retain, nonatomic)UICollectionView *myCollection;
@property (retain, nonatomic)NSMutableArray *cityArr;
@end

@implementation TotalCityViewController

- (void)dealloc
{
    [_myCollection release];
    [_cityArr release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    view.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.frame.size.height);
    self.navigationController.navigationBar.translucent = YES;
    UIImage *image = [UIImage imageNamed:@""];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    self.extendedLayoutIncludesOpaqueBars = YES;
    [self.navigationController.navigationBar setShadowImage:image];
    
    //夜间模式判断
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"night" object:nil];
}

- (void)changeColor:(NSNotificationCenter *)notification
{
    self.navigationController.navigationBar.barTintColor = BARCOLOR;
    self.myCollection.backgroundColor = CELLCOLOR;
    [self.myCollection reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"全部城市";
//    self.view.backgroundColor = [UIColor brownColor];

    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.myCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, BOUNDS.size.height+46) collectionViewLayout:layout];
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    [self.myCollection registerClass:[TotalCityCollectionViewCell class] forCellWithReuseIdentifier:@"TotalCityCollectionViewCell"];
    self.myCollection.backgroundColor = CELLCOLOR;
    self.myCollection.contentInset = UIEdgeInsetsMake(5, 5, 5, 5);
    [self.view addSubview:self.myCollection];
    [layout release];
    [_myCollection release];


    [self addHeader];
    [self addFooter];
}


#pragma mark --下拉刷新
- (void)addHeader
{
    
    __block TotalCityViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    
    self.cityArr = [NSMutableArray array];
    // 添加下拉刷新头部控件
    [self.myCollection addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.nextPage = 1;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getData:vc.nextPage];//重新请求数据
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myCollection headerBeginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    
    //    __unsafe_unretained typeof(self) vc = self;
    //__unsafe_unretained ViewController *vc = self;
    __unsafe_unretained TotalCityViewController *vc = self;
    // 添加上拉刷新尾部控件
    [self.myCollection addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.nextPage ++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc getData:self.nextPage];
    }];
}

- (void)getData:(NSInteger)nextPage
{
    [AppTools getAFN:[NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=866963025209810&track_app_version=6.5.5&track_app_channel=xiaomi&track_device_info=cancro&track_os=Android4.4.4&app_installtime=1439787107899&lat=38.883255&lon=121.544923&page=%ld&countryid=%ld", self.nextPage, self.countryId] block:^(id result) {
        if (self.isUpLoading == NO) {
            [self.cityArr removeAllObjects];
            
        }
        NSMutableDictionary *bigDic = result;
        NSMutableArray *arr = [bigDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in arr) {
            City *city = [[City alloc] init];
            [city setValuesForKeysWithDictionary:dic];
            [self.cityArr addObject:city];
        }
        [self.myCollection reloadData];
        [self.myCollection headerEndRefreshing];
        [self.myCollection footerEndRefreshing];
     }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cityArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TotalCityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TotalCityCollectionViewCell" forIndexPath:indexPath];
    cell.city = [self.cityArr objectAtIndex:indexPath.row];
    cell.backgroundColor = CELLCOLOR;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(BOUNDS.size.width/2-10, 200);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
